package com.bc.ljn.catwars;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

/**
 * Created by ljn on 2014/12/24.
 */
public class Enemy {
    //敌机的种类
    public int type;
    //狗
    public static final int TYPE_DOG = 1;
    public static final int TYPE_2 = 2;
    //敌机图片资源
    public Bitmap bmpEnemy;
    //敌机坐标
    public int x, y;
    //敌机每帧的宽高
    public int frameW;
    public int frameH;
    //敌机当前帧下标
    private int frameIndex;
    //敌机的移动速度
    private int speed;
    //判断敌机是否已经出屏
    public boolean isDead;

    //敌机的构造函数
    public Enemy(Bitmap bmpEnemy, int enemyType, int x, int y) {
        this.bmpEnemy = bmpEnemy;
        frameW = bmpEnemy.getWidth() / 2;
        frameH = bmpEnemy.getHeight();
        this.type = enemyType;
        this.x = x;
        this.y = y;

        //不同种类的敌机速度不同
        switch (type) {
            case TYPE_DOG:
                speed = 3;
                break;
            case TYPE_2:
                speed = 25;
                break;
        }
    }

    //敌机的绘图函数
    public void draw(Canvas canvas, Paint paint) {
        canvas.save();
        canvas.clipRect(x, y, x + frameW, y + frameH);
        canvas.drawBitmap(bmpEnemy, x - frameIndex * frameW, y, paint);
        canvas.restore();
    }

    //敌机逻辑AI
    public void logic() {
        //不断播放循环动画形成动画
        frameIndex++;
        if (frameIndex >= 2) {
            frameIndex = 0;
        }

        //不同种类AI逻辑
        switch (type) {
            case TYPE_DOG:
                if (isDead == false) {
                    //减速出现，加速返回
                    x += speed / 2;
                    y += speed;
                    if (x > GameView.screenW) {
                        isDead = true;
                    }
                }
                break;
            case TYPE_2:
                if (isDead == false) {
                    //减速出现，加速返回
                    speed -= 1;
                    y += speed;
                    if (y <= -200) {
                        isDead = true;
                    }
                }
                break;
        }
    }

    //敌机与主角子弹碰撞
    public boolean isCollisionWith(Bullet bullet) {
        int x2 = bullet.bulletX;
        int y2 = bullet.bulletY;
        int w2 = bullet.bmpBullet.getWidth();
        int h2 = bullet.bmpBullet.getHeight();
        if (x >= x2 && x >= x2 + w2) {
            return false;
        } else if (x <= x2 && x + frameW <= x2) {
            return false;
        } else if (y >= y2 && y >= y2 + h2) {
            return false;
        } else if (y <= y2 && y + frameH <= y2) {
            return false;
        }
        isDead = true;
        return true;
    }

}
